// pages/employees/list/index.js
import request from '../../../baseRequest'
import urls from '../../../requestUrl'
import { $Message } from '../../../components/base/index'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        employeeList: [],
        isHiddenCheck: true,
        actionsVisible: false,
        ids: [],
        actions: [{
                name: '邀请微信好友',
                icon: 'share',
                openType: 'share'
            },
            {
                name: '生成企业邀请码',
                icon: 'qrcode'
            }
        ],
        delActionVisible: false,
        delActions: [{
            name: '删除',
            color: '#ed3f14'
        }],
        centerPop: false
    },
    /**进入删除模式 */
    toDeleteMode() {
        this.setData({
            isHiddenCheck: false
        })
    },
    /**取消删除模式 */
    cancelDeleteMode() {
        this.setData({
            isHiddenCheck: true
        })
    },
    /**监听单选按钮选择事件 */
    checkboxChange(e) {
        console.log(e)
        this.data.ids = e.detail.value
            // if (this.data.isHiddenCheck) return
    },
    /**邀请员工 */
    invitalEmployee() {
        this.setData({
            actionsVisible: true
        })
    },
    /**取消展示Actions */
    handleActionCancel() {
        this.setData({
            actionsVisible: false
        })
    },
    /**处理Actions点击事件 */
    handleClickActionItem(e) {
        let _index = e.detail.index
        if (_index === 1) {
            this.setData({
                actionsVisible: false
            })
            setTimeout(() => {
                this.setData({
                    centerPop: true
                })
            }, 300)
        }
    },
    /**处理点击删除按钮事件 */
    handleToDelete() {
        this.setData({
            delActionVisible: true
        })
    },
    /**取消展示DeleteActions */
    handleDeleteActionCancel() {
        this.setData({
            delActionVisible: false
        })
    },
    /**处理DeleteActions点击事件 */
    handleClickDelete(e) {
        //批量删除
        console.log(e)
        if (this.data.ids.length === 0) return
        let param = this.data.ids
        request.postRequest(urls.leader.delEmployess, param, (res) => {
            this.getEmployeeList()
        }, () => {})
        this.handleDeleteActionCancel()
    },

    /**获取openid */
    getUserOpenid() {
        let openid = getApp().getUserOpenid()
        this.setData({
            openid
        })
    },
    /**获取员工列表 */
    getEmployeeList() {
        let url = `${urls.leader.getEmployeeList}/${this.data.openid}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                employeeList: res
            })
        }, () => {})
    },
    /**关闭邀请码弹窗 */
    handleCloseCenterPopup() {
        this.setData({
            centerPop: false
        })
    },
    /**获取邀请码链接 */
    getInviteCodeByUser() {
        let url = `${urls.leader.getInviteCode}/${this.data.openid}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                inviteCode: urls.baseUrl + res.companyInviteCode
            })
        }, () => {})
    },
    /**保存邀请码 */
    handleSaveCode() {
        if (this.data.inviteCode) {
            wx.getImageInfo({
                src: this.data.inviteCode,
                success: (res) => {
                    wx.saveImageToPhotosAlbum({
                        filePath: res.path,
                        success: (r) => {
                            this.showTipsMessage('邀请码保存成功，请前往手机相册查看')
                            setTimeout(() => {
                                this.setData({
                                    centerPop: false
                                })
                            }, 300)
                        },
                        fail: () => {
                            this.showErrorMessage('邀请码保存至本地失败')
                        }
                    })
                },
                fail: () => {
                    this.showErrorMessage('获取邀请码信息有误，无法保存')
                }
            })
        } else {
            this.showErrorMessage('邀请码信息有误')
        }
    },
    /**展示信息提示 */
    showTipsMessage(msg) {
        $Message({
            content: msg,
            type: 'info'
        })
    },

    /**展示错误提示 */
    showErrorMessage(msg) {
        $Message({
            content: msg,
            type: 'error'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // this.getUserOpenid()
        this.data.openid = getApp().getUserOpenid()
        this.getEmployeeList()
        this.getInviteCodeByUser()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(res) {
        console.log('share', res)
        return getApp().getShareAppParam('邀请你加入!!!', `pages/employees/join/index?inviteOpenid=${this.data.openid}`)
            // return {
            //     title: '邀请你加入',
            //     imageUrl: '/assets/share-bg.png',
            //     path: "pages/employees/join/index?inviteOpenid=" + this.data.openid
            // }
    }
})